<template>
  <div id="market_gameticket_times">
    <div class="search-box">
      <Row type="flex">
        <Row type="flex" justify="start" class="search-item-left">
          <Col class="search-item"><span class="search-label">局数：</span><Input placeholder="请输入" @on-keyup='filterSpace' @on-blur="filterSpace" v-model='searchTimes'></Input></Col>
          <Col class="search-item">
            <span class="search-label">创建时间：</span>
            <DatePicker type="datetimerange" placeholder="请选择" placement="bottom-end" :editable='false' format="yyyy-MM-dd HH:mm:ss" class="data-time-style" style="width:280px;" :value="dateVal" @on-change="daterangeChange"></DatePicker>
          </Col>
          <Col class="search-item">
          <Button class="search-btn"
                  type='primary'
                  @click="search"
                  >查询</Button>
          </Col>
          <Col class="search-item">
          <Button class="search-btn"
                @click="reset">重置</Button>
          </Col>
        </Row>
        <Row type="flex" justify="end" class="search-item-right">
          <Col class="search-item"><Button class="search-btn" type="primary" @click="skipAdd" v-if="hasPermission('moms:product:roundcreate')">新增</Button></Col>
        </Row>
      </Row>
    </div>
    <Table stripe class="table-list" :columns="columns" :data="data">
      <template slot-scope="{ row }" slot="action">
        <i class="iconsys iconedit" title="编辑" @click='skipEdit(row)' v-if="hasPermission('moms:product:roundupdate')"></i>
        <Poptip confirm title="你确定要删除吗？" @on-ok="sureDel(row)"
        v-if="hasPermission('moms:product:rounddelete')"
        style="text-align:left">
          <i class="iconsys icondel" title="删除" ></i>
        </Poptip>
      </template>
    </Table>
    <Row type='flex' justify="end" style='padding: 20px'>
      <Col>
        <Page :total="total"
              :page-size='pageSize'
              :current='pageIndex'
              :page-size-opts='[20,50,100,200]'
              show-total
              show-sizer
              @on-change='changePageIndex'
              @on-page-size-change='changePageSize' />
      </Col>
    </Row>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getPriceLists, deletePrice, priceVerify } from '@/api/market/game-ticket/priceTimes.js'
export default {
  name: 'commodity_gameticket_times',
  data () {
    return {
      searchTimes: '',
      total: 0,
      pageSize: 20,
      pageIndex: 1,
      loading: false,
      tableHeight: 0,
      dateVal: '',
      columns: [
        {
          title: '编号',
          key: 'configCode'
        },
        {
          title: '局数',
          key: 'configValue'
        },
        {
          title: '修改时间',
          key: 'modifyTime'
        },
        {
          title: '创建时间',
          key: 'createTime'
        },
        {
          title: '操作人',
          key: 'modifierName'
        },
        {
          title: '备注',
          key: 'remark',
          width: 220
        }
      ],
      data: []
    }
  },
  computed: {
    ...mapState({
      merchantId: state => state.user.merchantId
    })
  },
  watch: {
    '$route' (newRoute) {
      if (newRoute.name === 'commodity_gameticket_times') {
        this.search()
      }
    }
  },
  mounted () {
    this.getData()
    if (this.hasPermission(['moms:product:roundupdate', 'moms:product:rounddelete'])) {
      this.columns.push({
        title: '操作',
        slot: 'action',
        width: 150
      })
    }
  },
  methods: {
    filterSpace () {
      this.searchTimes = this.searchTimes.replace(/\D/g, '')
    },
    skipAdd () {
      this.$router.push({ name: 'commodity_gameticket_times_create' })
    },
    skipEdit (row) {
      priceVerify({
        merchantId: this.merchantId,
        id: row.id
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.$router.push({
            name: 'commodity_gameticket_times_edit',
            query: {
              id: row.id
            }
          })
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    },
    getData () {
      this.loading = true
      this.tableHeight = 0
      getPriceLists({
        merchantId: this.merchantId,
        configType: 2,
        configValue: this.searchTimes,
        startTime: this.dateVal ? this.dateVal[0] : this.dateVal,
        endTime: this.dateVal ? this.dateVal[1] : this.dateVal,
        offset: (this.pageIndex - 1) * this.pageSize,
        limit: this.pageSize
      }).then(res => {
        this.loading = false
        const result = res.data || {}
        if (result.status === 200) {
          this.data = result.data || []
          this.total = result.totalCount || 0
          if (this.data.length > 10) {
            this.tableHeight = 520
          }
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
        this.loading = false
      })
    },
    sureDel (row) {
      deletePrice({
        merchantId: this.merchantId,
        id: row.id
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.$Message.success('删除成功！')
          this.getData()
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    },
    daterangeChange (daterange) {
      this.dateVal = daterange
    },
    search () {
      this.pageIndex = 1
      this.getData()
    },
    reset () {
      this.searchTimes = ''
      this.dateVal = ''
      this.search()
    },
    changePageIndex (page) {
      this.pageIndex = page
      this.getData()
    },
    changePageSize (size) {
      this.pageSize = size
      this.search()
    }
  }
}
</script>

<style lang='less'>
  #market_gameticket_times {
    .data-time-style {
      .ivu-input-wrapper {
        width: 280px;
      }
    }
  }
</style>
